package com.newtouch.xcd.admin.config;


import com.newtouch.xcd.common.constants.RedisKeyDict;
import com.newtouch.xcd.common.utils.DateUtils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ConfigurationProperties(prefix = SequenceConfig.SEQUENCE_CONFIG_PREFIX)
@Data
public class SequenceConfig {

    public static final String SEQUENCE_CONFIG_PREFIX = "sequence";
    private List<Sequence> list;


    @Data
    public static class Sequence {

        /**
         * 类型，作为redis-key
         */
        private String type;

        /**
         * 业务编号的前缀
         */
        private String prefix;

        /**
         * 时间格式
         */
        private String pattern;

        /**
         * 序列号长度， 不足补0
         */
        private int length;

        public String getCacheKey() {
            return RedisKeyDict.XCD.SEQUENCE.getCode() + this.type;
        }

        public String next(Long seq) {
            String timeStr = DateUtils.format(DateUtils.now(), "yyyy-MM-dd HH:mm:ss", this.pattern);
            String content = String.format("%0" + this.length + "d", seq);
            StringBuilder sb = new StringBuilder();
            sb.append(this.prefix)
                    .append(timeStr)
                    .append(content);
            return sb.toString();
        }
    }

}
